﻿using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.User;
using CE.Global;
using CE.Database;
using CE.UserAccess;

public partial class User_Account_ChangePassword : System.Web.UI.Page
{
    string PageCode = "CXTHRCPW";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/ManagementSecureLogin.aspx");
        }
        if (!IsPostBack)
        {
            ClearProcess();
            if (Request.Params["menu"] != null)
            {
                if (Request.Params["menu"] == "1")
                {
                    lblMessage.Text = "Successfully Updated!";
                    lblError.Text = "";
                    btnSignOut.Visible = false;
                }
                else
                {
                    btnSignOut.Visible = true;
                }
            }
        }
        if (Convert.ToInt64(Session[General.UserId].ToString()) != 1)
        {
            CheckPageAccess();
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {            
            AccountDB _accountdb = new AccountDB();
            Account _account = _accountdb.CheckEmail(Session[General.UserEmail].ToString(), 100);
            if (_account != null)
            {
                if (txtOldPassword.Text == _account.AuthCode)
                {
                    lblMessage.Text = "";
                    lblError.Text = "";
                    Int64 _result = -1;
                    Int64 _UserId = Convert.ToInt64(Session[General.UserId].ToString());
                    string _AuthCode = txtNewPassword.Text;

                    _result = _accountdb.UpdatePassword(_UserId, _AuthCode);
                    if (_result > 0)
                    {
                        AccountStatusDB _accountstatusdb = new AccountStatusDB();
                        if (_accountstatusdb.UpdatePasswordChangedDate(_UserId, CE.Global.General.GetUTCDateTime()))
                        {
                            Response.Redirect("~/user/account/ChangePassword.aspx?menu=1");                           
                            //lblMessage.Text = "Successfully Updated!";
                            //lblError.Text = "";
                        }
                        else
                        {
                            lblMessage.Text = "";
                            lblError.Text = "Successfully Updated! But Date Not Updated";
                        }
                    }
                    else
                    {
                        lblError.Text = "Updated Failed!";
                        lblMessage.Text = "";
                    }
                }
                else
                {
                    lblError.Text = "Invalid Old Password!";
                    lblMessage.Text = "";
                }
            }     
        }
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        ClearProcess();
    }

    protected void btnSignOut_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/LogOut.aspx");
    }

    private void ClearProcess()
    {
        txtOldPassword.Text = "";
        txtNewPassword.Text = "";
        txtConfirmPassword.Text = "";
        lblError.Text = "";
        lblMessage.Text = "";
    }

    private void CheckPageAccess()
    {
        Int64 PageAccess = General.GetPageAccess(Convert.ToInt64(Session[General.UserId].ToString()), PageCode);
        if (PageAccess == 0)
        {
            Response.Redirect("~/UserMessage.aspx?menu=0&mt=UM&ec=101");
        }
        else if (PageAccess > 0)
        {
            EnablePageAccess(PageAccess);
        }
    }

    private void EnablePageAccess(Int64 PageAccess)
    {
        if (PageAccess == 100)
        {
            btnSubmit.Enabled = false;
        }
        else if (PageAccess == 300)
        {
            btnSubmit.Enabled = true;
        }
    }

}
